package org.mzyyn.qr.controller;

import jakarta.validation.Valid;
import org.mzyyn.qr.constants.AppletsApiUrlMapping;
import org.mzyyn.qr.dto.user.Code2sessionLoginDTO;
import org.mzyyn.qr.dto.user.UserProfileLoginDTO;
import org.mzyyn.qr.service.user.UserAppletsService;
import org.mzyyn.qr.service.user.UserService;
import org.mzyyn.qr.vo.user.LoginUserInfoVO;
import org.mzyyn.qr.vo.user.LoginUserVO;
import org.qr.xiaocao.common.entity.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lipeng
 * @since 2024-06-12
 */
@RestController
public class UserWxAppletsController {

    @Autowired
    UserService userService;

    /**
     * 登录
     * @param code2sessionLoginDTO
     * @return
     */
    @PostMapping(AppletsApiUrlMapping.USER_CODE_TWO_SESSION_LOGIN)
    public BaseResult<LoginUserInfoVO>  appletUnionidLogin(
            @RequestBody  Code2sessionLoginDTO code2sessionLoginDTO
    ){
        return BaseResult.ok(userService.appletUnionidLogin(code2sessionLoginDTO));
    }

    /**
     * 注册或登录
     * @param userProfileLoginDTO
     * @return
     */
    @PostMapping(AppletsApiUrlMapping.USER_USER_PROFILE_REGIST_OR_LOGIN)
    public BaseResult<LoginUserInfoVO>  appletProfilePhoneLogin(
            @Valid @RequestBody UserProfileLoginDTO userProfileLoginDTO
    ){
        return BaseResult.ok(userService.registerAndLogin(userProfileLoginDTO));
    }

    /**
     * 获取用户信息
     * @return
     */
    @PostMapping(AppletsApiUrlMapping.USER_INFO_GET)
    public BaseResult<LoginUserVO>  getUserInfo(){
        return BaseResult.ok(userService.getUserInfo());
    }

    /**
     * 获取h5的code实际就是openid
     * @return
     */
    @PostMapping(AppletsApiUrlMapping.USER_H5_CODE_GET)
    public BaseResult<String>  getH5Code(){
        return BaseResult.ok(userService.getH5Code());
    }


}
